package com.yinxin.wudasuanfa;

/**
 * @author yinxin
 * @version 1.0
 * @Description:
 * @date 2022/4/1617:48
 */
public class Day2 {
    /**
     * 二分查找
     *
     * @param
     * @param
     * @return
     */
    /*public static int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        int mid;
        while (left <= right) {
            mid = (left + right) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }*/
    public static void main(String[] args) {
        //====二分查找排序====
        //=================二分查找=======================
        /*int[] nums = new int[]{-1, 0, 3, 4, 6, 10, 13, 14};
        int target = -1;
        int search = search(nums, target);
        System.out.println(search);*/
        //=================二维数组中的查找=======================
        /*int[][] arrnum = new int[][]{{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
        int target = 15;
        boolean searcharr = searcharr(arrnum, target);
        System.out.println(searcharr);*/
        //=================寻找峰值=======================
        int[] nums = new int[]{2, 4, 1, 2, 7, 8, 4};
        int peakElement = findPeakElement(nums);
        System.out.println(peakElement);
    }

    //二维数组的二分查找
    /*private static boolean searcharr(int[][] arrnum, int target) {
        boolean flag = false;
        for (int i = 0; i < arrnum.length; i++) {
            int search = search(arrnum[i], target);
            if (search != -1) {
                flag = true;
                System.out.println("行=" + ++i + "   列=" + ++search);
                return flag;
            }
        }
        return flag;
    }*/

    //寻找峰值
    public static int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        int mid;
        while (left < right) {
            mid = (left + right) / 2;
            if (nums[mid] > nums[mid + 1]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }

}
